/**	
 * Author : Mht
 * Desc : For 2016 RDM 
 */
package service.message;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import common.Constants;
import entity.message.response.Article;
import entity.message.response.BaseMessage;
import entity.message.response.NewsMessage;
import entity.message.response.TextMessage;
import lombok.extern.log4j.Log4j;

/**
 * @author Mht
 * @date May 30, 2016
 */
@Log4j
@Service("coreService")
public class MessageProcessService {

	@Resource
	private MessageService messageService;

	public String processRequest(HttpServletRequest request) {
		String respMessage = "";
		try {
			// XML请求解析
			Map<String, String> requestMap = messageService.parseXML(request);

			// 响应XML消息
			respMessage = responseXML(requestMap);
		} catch (Exception e) {
			log.error("Process Request Error : " + e.getMessage());
		}
		log.info("Resp Message Content : " + respMessage);
		return respMessage;
	}

	private String responseXML(Map<String, String> requestMap) {

		// 发送方账号(OPEN_ID)
		String fromUserName = requestMap.get("FromUserName");
		// 接收方公众号
		String toUserName = requestMap.get("ToUserName");
		// 请求消息类型
		String reqMsgType = requestMap.get("MsgType");
		// 请求消息内容
		String reqContent = requestMap.get("Content");
		// 设置默认响应消息
		String respContent = "请求处理异常,请稍候再试!";

		BaseMessage message = new BaseMessage();
		message.setToUserName(toUserName);
		message.setFromUserName(fromUserName);
		message.setMsgType(Constants.RESP_MESSAGE_TYPE_TEXT);
		message.setFuncFlag(0);

		if (reqMsgType.equals(Constants.REQ_MESSAGE_TYPE_TEXT)) {
			respContent = "您发送的是文本消息";
			// 响应文本消息
			TextMessage textMessage = new TextMessage();
			textMessage.setToUserName(toUserName);
			textMessage.setFromUserName(fromUserName);
			textMessage.setMsgType(Constants.RESP_MESSAGE_TYPE_TEXT);
			textMessage.setFuncFlag(0);
			textMessage.setContent(reqContent + " " + respContent);
			return messageService.responseTextMessage(textMessage);
		} else if (reqMsgType.equals(Constants.REQ_MESSAGE_TYPE_IMAGE)) {
			NewsMessage newsMessage = new NewsMessage();
			newsMessage.setToUserName(toUserName);
			newsMessage.setFromUserName(fromUserName);
			newsMessage.setMsgType(Constants.RESP_MESSAGE_TYPE_TEXT);
			newsMessage.setFuncFlag(0);
			newsMessage.setArticleCount(2);
			newsMessage.getArticles().add(new Article("Mht", "I Love You", "http://7fde7a8c.ngrok.io/weixin/love.jpg",
					"http://7fde7a8c.ngrok.io/weixin/loveDetail"));
			newsMessage.getArticles().add(new Article("CY", "I Love You", "http://7fde7a8c.ngrok.io/image/love.jpg",
					"http://7fde7a8c.ngrok.io/weixin/loveDetail"));
			respContent = "您发送的是图片消息";
			return messageService.responseNewsMessage(newsMessage);
		} else if (reqMsgType.equals(Constants.REQ_MESSAGE_TYPE_LINK)) {
			respContent = "您发送的是链接消息";
		} else if (reqMsgType.equals(Constants.REQ_MESSAGE_TYPE_LOCATION)) {
			respContent = "您发送的是位置消息";
		} else if (reqMsgType.equals(Constants.REQ_MESSAGE_TYPE_VOICE)) {
			respContent = "您发送的是声音消息";
		} else if (reqMsgType.equals(Constants.REQ_MESSAGE_TYPE_EVENT)) {
			String eventType = requestMap.get("Event");
			if (eventType.equals(Constants.EVENT_TYPE_SUBSCRIBE)) {
				respContent = "谢谢您的关注";
			} else if (eventType.equals(Constants.EVENT_TYPE_UNSUBSCRIBE)) {
				respContent = "您已经取消该关注";
			} else if (eventType.equals(Constants.EVENT_TYPE_CLICK)) {
				respContent = "您发送的点击事件";
			}
		}
		return respContent;
	}

}
